/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.redkale.util;

import java.util.*;
import java.util.concurrent.*;

/**
 * 简单的CompletableFuture与Flow.Subscriber的结合类。
 *
 * <p>
 * 详情见: https://redkale.org
 *
 * @since 2.5.0
 * @param <T> T
 */
public class SubscriberListFuture<T> extends CompletableFuture<List<T>> implements Flow.Subscriber<T> {

    protected List<T> rs;

    @Override
    public void onSubscribe(Flow.Subscription s) {
        s.request(Integer.MAX_VALUE);
    }

    @Override
    public void onNext(T item) {
        if (rs == null) rs = new ArrayList<>();
        rs.add(item);
    }

    @Override
    public void onError(Throwable t) {
        completeExceptionally(t);
    }

    @Override
    public void onComplete() {
        complete(rs == null ? new ArrayList<>() : rs);
    }

}
